package com.squirrel.michale;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/25 6:32 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode2207 {

    public long maximumSubsequenceCount(String text, String pattern) {
        char first = pattern.charAt(0);
        char second = pattern.charAt(1);
        long sum = 0;
        int firstCount = 0;
        int secondCount = 0;
        for (int i = text.length() - 1; i >= 0; i--) {
            char at = text.charAt(i);
            if (at == first) {
                sum += secondCount;
                firstCount++;
            }
            if (at == second) {
                secondCount++;
            }
        }
        // 对插入最后的一个字符后增加数进行计算 第一个字符多就在text的开始添加，第二个字符多就在text的结尾添加
        return sum + Math.max(firstCount, secondCount);
    }
}
